package org.stars.daostars.core.runtime.columns;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.stars.daostars.conversion.TypeHandler;

public class TypeHandlerColumn implements ColumnProcessor {

	public static ColumnProcessor	instance	= new TypeHandlerColumn();

	/**
	 * Nel caso di errore dentro l'handler notifica l'errore.
	 * 
	 * @see org.stars.daostars.core.runtime.columns.ColumnProcessor#execute(java.sql.ResultSet,
	 *      int, java.lang.Class, it.insiel.daostars.conversion.TypeHandler)
	 */
	@Override
	public Object execute(ResultSet rs, int index, Class<?> propType, TypeHandler handler) throws SQLException {
		try {
			return handler.databaseToJava(rs.getObject(index));
		} catch (Exception e) {
			String temp = rs.getObject(index) == null ? "{null}" : rs.getObject(index).toString();
			String sHandler = handler != null ? handler.getClass().toString() : "{Handler not defined}";
			String msg = "Found error(s) in typeHandler " + sHandler + " with input: " + temp;
			throw (new SQLException(msg));
		}
	}

}
